package com.example.musicaiap.data.database

import androidx.room.Entity
import androidx.room.PrimaryKey
import com.example.musicaiap.domain.model.Playlist

/**
 * 播放列表数据库实体
 * 用于Room数据库存储播放列表信息
 */
@Entity(tableName = "playlists")
data class PlaylistEntity(
    @PrimaryKey
    val id: String,
    val name: String,
    val description: String?,
    val coverUrl: String,
    val creator: String,
    val isOfficial: Boolean,
    val isLocal: Boolean = false, // 是否为本地播放列表
    val createdAt: Long = System.currentTimeMillis(),
    val updatedAt: Long = System.currentTimeMillis()
) {
    /**
     * 转换为领域模型
     * @param songCount 该播放列表包含的歌曲数量
     */
    fun toDomainModel(songCount: Int = 0): Playlist {
        return Playlist(
            id = id,
            name = name,
            description = description,
            coverUrl = coverUrl,
            songCount = songCount,
            creator = creator,
            isOfficial = isOfficial
        )
    }
    
    companion object {
        /**
         * 从领域模型创建数据库实体
         */
        fun fromDomainModel(playlist: Playlist, isLocal: Boolean = false): PlaylistEntity {
            return PlaylistEntity(
                id = playlist.id,
                name = playlist.name,
                description = playlist.description,
                coverUrl = playlist.coverUrl,
                creator = playlist.creator,
                isOfficial = playlist.isOfficial,
                isLocal = isLocal
            )
        }
        
        /**
         * 创建本地音乐播放列表
         */
        fun createLocalPlaylist(): PlaylistEntity {
            return PlaylistEntity(
                id = "local_playlist",
                name = "本地音乐",
                description = "设备上的音乐文件",
                coverUrl = "",
                creator = "系统",
                isOfficial = false,
                isLocal = true
            )
        }
        
        /**
         * 创建收藏音乐播放列表
         */
        fun createFavoritePlaylist(): PlaylistEntity {
            return PlaylistEntity(
                id = "favorites",
                name = "我喜欢",
                description = "收藏的歌曲",
                coverUrl = "",
                creator = "系统",
                isOfficial = false,
                isLocal = false
            )
        }
    }
} 